package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 利用反射机制调用方法
 */
public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.watchTV();


        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名:");
        String className = scanner.nextLine();
        System.out.println("请输入方法名:");
        String methodName = scanner.nextLine();


        Class cls = Class.forName(className);
        //实例化
        Object obj = cls.newInstance();


        Method method = cls.getMethod(methodName);
        //2.2通过方法对象执行该方法
        method.invoke(obj);
    }
}
